package com.li.springbootproject.service.spring;

import com.li.StarterService;
import com.li.springbootproject.spring.beanfactorypostprocessor.CarEngine;
import com.li.springbootproject.spring.beaninit.InitMethodBean;
import com.li.springbootproject.spring.beaninit.InitializingBeanExample;
import com.li.springbootproject.spring.createbean.PersonBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2021/12/29 21:33
 */
@Service
public class SpringServiceImpl implements SpringService {

    private static final Logger logger = LoggerFactory.getLogger(SpringServiceImpl.class);

    @Autowired
    private PersonBean personBean;

    @Autowired
    private CarEngine carEngine;

    @Autowired
    private InitializingBeanExample initializingBeanExample;

    @Autowired
    private InitMethodBean initMethodBean;

    @Autowired
    private StarterService starterService;

    @Override
    public void testOne(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        // 支持中文
        PrintWriter writer = response.getWriter();
        try {
            writer.print("测试的例子");
            int i = 5 / 0;
            writer.print("测试的例子1");
        } catch (Exception e) {
            logger.error("{}", e);
            writer.print("出错：" + e.getMessage());
        } finally {
            writer.close();
        }
    }

    @Override
    public void testTwo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        // 使用了二进制流，但是二进制流是没有对字符进行编码的，它默认使用并只能使用ISO-8859-1进行编码，而该编码方式无法表示中文字符，因此会出现上述的错误。
        ServletOutputStream outputStream = response.getOutputStream();
        try {
            outputStream.print("测试的例子");
            int i = 5 / 0;
            outputStream.print("测试的例子1");
        } catch (Exception e) {
            logger.error("{}", e);
            outputStream.print("出错：" + e.getMessage());
        } finally {
            outputStream.close();
        }
    }

    @Override
    public void testThree(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            // 使用了二进制流，但是二进制流是没有对字符进行编码的，它默认使用并只能使用ISO-8859-1进行编码，而该编码方式无法表示中文字符，因此会出现上述的错误。
            byteArrayOutputStream.writeTo(response.getOutputStream());
            response.getOutputStream().print("测试的例子");
            response.getOutputStream().print("测试的例子1");
        } catch (Exception e) {
            response.getWriter().write("test4");
            logger.error("{}", e);
        }
    }

    @Override
    public void testFour() {
        logger.info(personBean.toString());
        logger.info(initializingBeanExample.toString());
        logger.info(initMethodBean.toString());
        carEngine.fire();
        carEngine.close();
    }

    @Override
    public void testSix() {
        String[] split = starterService.split(",");
        logger.info(String.join(",", split));
    }
}
